home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / CAuthentication 1.0 / CAuthentication.cp < prev    next >
Text File  |  1994-04-06  |  5KB  |  167 lines

  1. /******************************************************************************
  2.  CAuthentication.cp
  3.  
  4.                             The PowerTalk Authentication Class
  5.                                 
  6.     SUPERCLASS = CObject
  7.     
  8.     Copyright © 1993 Marty Wachter. All rights reserved.
  9.     
  10.     Portions Copyright © 1993 Apple Computer, Inc.  All rights reserved.
  11.     Portions Copyright © 1989 Symantec Corporation. All rights reserved.
  12.  
  13.     Original Author:     Martin R. Wachter        email:    mrw@welchgate.welch.jhu.edu
  14.     Created:            8/4/93                    by:        mrw            TCL Version:    1.1.3
  15.     Modified:            4/6/94                    by:        mrw            TCL Version:    1.1.3
  16.  
  17.  
  18.     Version change history:
  19.     
  20.     1.0        Initial release.
  21.  
  22.  
  23. Sample Usage.  Place in your CApplication subclass.  Don't forget to include 
  24.                the STR# resources in your project!
  25.     
  26. // PowerTalk User Prompts
  27. #define     kGuest        "\pLog in as a Guest..."
  28. #define     kSpecific    "\pPlease enter your User Name and Password for access"
  29. #define        kLocal        "\pPlease enter your PowerTalk Access Code"
  30.     
  31.     
  32.     ***sample usage***
  33.     
  34.     itsAuth = new (CAuthentication);
  35.     
  36.     itsAuth->IAuthentication(kGuest,kSpecific,kLocal,TRUE,TRUE,TRUE);
  37.     
  38.     if (itsAuth->hasOCE)
  39.         itsAuth->GetUserID();
  40.     
  41.     if (itsAuth->status)
  42.         DoCommand(cmdQuit);
  43.         
  44.     ******************
  45.  
  46. ******************************************************************************/
  47.  
  48. #include "CAuthentication.h"
  49.  
  50. #include "TBUtilities.h"
  51. #include <GestaltEqu.h>
  52.  
  53. extern     CError        *gError;                        //Global TCL Error handler
  54.  
  55. /******************************************************************************
  56.  IAuthentication
  57.  
  58.         Initialize an AOCE object
  59.  ******************************************************************************/
  60.  
  61. void    CAuthentication::IAuthentication(ConstStr255Param         aGuestPrompt,
  62.                                          ConstStr255Param         aSpecificPrompt,
  63.                                          ConstStr255Param         aLocalPrompt,
  64.                                          Boolean                aAllowGuest,
  65.                                          Boolean                aAllowSpecific,
  66.                                          Boolean                aAllowLocal)
  67. {
  68.  
  69. long                    gestaltResponse;
  70.  
  71.     hasOCE = FALSE;// assume AOCE is not present yet
  72.     status = noErr;// assume noErr yet
  73.     
  74.     // Initialize the authentication prompts with TCL string routines
  75.     CopyPString(aGuestPrompt, guestPrompt);
  76.     CopyPString(aSpecificPrompt, specificPrompt);
  77.     CopyPString(aLocalPrompt, localPrompt);
  78.         
  79.     allowGuest =     aAllowGuest;
  80.     allowSpecific = aAllowSpecific;
  81.     allowLocal =     aAllowLocal;
  82.     
  83.     //  Make sure this Macintosh supports AOCE by means of the Gestalt Mgr.
  84.     status = Gestalt(gestaltOCEToolboxAttr, &gestaltResponse);
  85.         
  86.     switch (status) {
  87.         case noErr:
  88.             hasOCE = TRUE;
  89.             break;
  90.         case kOCEToolboxNotOpen:
  91.             hasOCE = TRUE;
  92.             break;
  93.         case gestaltUndefSelectorErr:
  94.             gError->PostAlert(kAuthenticationStrs, kAOCENotInstalled);
  95.             hasOCE = FALSE;
  96.             break;
  97.         default:
  98.             gError->PostAlert(kAuthenticationStrs, kAOCENotInstalled);
  99.             hasOCE = FALSE;
  100.             break;
  101.     }
  102.     
  103. }
  104.  
  105.  
  106. /******************************************************************************
  107.  GetUserID
  108.  
  109.         Get the user authentication ID from the PowerTalk Manager.
  110.         
  111.  ******************************************************************************/
  112.  
  113. void    CAuthentication::GetUserID(void)
  114. {
  115. AuthParamBlock            authParamBlock;
  116. SDPIdentityKind            allowedKind = 0;
  117.  
  118.  
  119.         //
  120.         // OCE Setup: get the user's local authentication identity. If it's
  121.         // already set, we can do this silently. If it's not set (the system
  122.         // has just been started), we'll prompt for the user's identity
  123.         // and password.
  124.         //
  125.         // The only expected error is "Cancel" from SDPPromptForIdentity.
  126.         //
  127.          
  128.         status = AuthGetLocalIdentity(
  129.                 &authParamBlock,            // Parameter block
  130.                 FALSE                        // Synchronous
  131.             );
  132.         
  133.         if (status == noErr) {
  134.             
  135.         // The user identity has already been specified.
  136.              
  137.             userIdentity = authParamBlock.getLocalIdentityPB.theLocalIdentity;
  138.         }
  139.  
  140.         else if (status == kOCELocalAuthenticationFail) {
  141.         
  142.         // No user identity has been specified. Ask for the user to specify
  143.         // the local identity.
  144.          
  145.             if (allowGuest) 
  146.                 allowedKind = ( allowedKind | kSDPGuestMask);
  147.             if (allowSpecific)
  148.                 allowedKind = ( allowedKind | kSDPSpecificIdentityMask);
  149.             if (allowLocal)
  150.                 allowedKind = ( allowedKind | kSDPLocalIdentityMask);
  151.             
  152.             status = SDPPromptForID(
  153.                     &userIdentity,            // AuthIdentity id
  154.                     guestPrompt,            // Default guest prompt
  155.                     specificPrompt,            // Default specific prompt
  156.                     localPrompt,            // Default local prompt
  157.                     NULL,                    // RString            *recordType
  158.                     allowedKind,            // SDPIdentityKind    permittedKinds
  159.                     &selectedKind,            // SDPIdentityKind    *selectedKind
  160.                     NULL,                    // RecordID            *loginFilter
  161.                     0                        // SDPLoginFilterKind filterKind                        
  162.                 );
  163.             if (status != noErr){
  164.                 gError->PostAlert(kAuthenticationStrs, kNotAuthorizedUser);
  165.             }
  166.         }
  167. }